import Scene from "../Scene";
import { TLayout, TButton, MoveInfo } from "../SceneControl";
import MainPageScene from "./MainPageScene";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CivilizationPanelScene extends MainPageScene {
    upLayout: TLayout;
    scroll: cc.ScrollView;

    protected onEnable(): void {
        this.scroll = cc.find("science/scrollView", this.node).getComponent(cc.ScrollView);
        super.onEnable();
        this.scheduleOnce(this.refresh, 0.1);
    }

    getUpLayout(): TLayout {
        this.upLayout = new TLayout(this.node.name);
        let btns = this.btns;
        this.upLayout.buttonList = btns;
        this.upLayout.site.row = btns.length - 1; 
        return this.upLayout;
    }

    refresh() {
        this.upLayout.buttonList = this.btns;
        this.tscene.apply();
    }

    get btns() {
        let btnNodes = this.scroll.content.children.map((node) => node.getChildByName("btnUpgrade"));
        let btns = Scene.sortNodesIntoGrid(btnNodes, 100, true) as TButton[][];
        let fingerIn = (moveinfo: MoveInfo) => {
            this.scroll.scrollToPercentVertical(1 - (moveinfo.targetLayout.site.row / (this.upLayout.buttonList.length - 1)), 0.2);
        }
        btns.forEach((row) => {
            row.forEach((btn) => {
                btn.addBeforeFingerInEvent(fingerIn);
            })
        })
        return btns;
    }
}
